package com.example.barcodeapp.ui.wode.activity;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.barcodeapp.R;
import com.example.barcodeapp.base.BaseActivity;
import com.example.barcodeapp.interfaces.IBasePersenter;
import com.example.barcodeapp.interfaces.own.IOwn;
import com.example.barcodeapp.persenter.own.DingDanPresenter;
import com.example.barcodeapp.ui.wode.bean.DingDanBean;
import com.example.barcodeapp.ui.wode.bean.UpDateNameBean;
import com.example.barcodeapp.ui.wode.fragment.HomeFragment;
import com.example.barcodeapp.ui.wode.fragment.PaymentFragment;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class OrderformActivity extends BaseActivity<IOwn.DingDanPresenter> implements IOwn.DingDanView {
    @BindView(R.id.orderform_tab)
    TabLayout orderformTab;
    @BindView(R.id.orderform_vp)
    ViewPager orderformVp;
    @BindView(R.id.orderfrom_img)
    ImageView orderformImg;
    private OrderformFragmentAdapter adapter;

    @Override
    protected int getLayout() {
        return R.layout.activity_orderform;
    }

    @Override
    protected void initView() {
        //返回
        orderformImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected DingDanPresenter initPersenter() {
        return new DingDanPresenter();
    }

    @Override
    protected void initData() {
        persenter.getDingDan("1", "1", null, null);//课程订单：未付款列表
    }

    /**
     * 订单数据
     *
     * @param dingDanBean
     */
    @Override
    public void getDingDanBean(DingDanBean dingDanBean) {
        List<DingDanBean.DataBean> data = dingDanBean.getData();
        ArrayList<Fragment> list = new ArrayList<>();
        list.add(new HomeFragment(data));
        list.add(new PaymentFragment(data));
        adapter = new OrderformFragmentAdapter(getSupportFragmentManager(), list);
        orderformVp.setAdapter(adapter);
        orderformTab.setupWithViewPager(orderformVp);
        orderformTab.getTabAt(0).setCustomView(getTabView("课程订单", R.drawable.selector_orderform));
        orderformTab.getTabAt(1).setCustomView(getTabView("活动订单", R.drawable.selector_orderform1));
    }


    private class OrderformFragmentAdapter extends FragmentStatePagerAdapter {
        private ArrayList<Fragment> list;

        public OrderformFragmentAdapter(@NonNull FragmentManager fm, ArrayList<Fragment> list) {
            super(fm);
            this.list = list;
        }

        @NonNull
        @Override
        public Fragment getItem(int position) {
            return list.get(position);
        }

        @Override
        public int getCount() {
            return list.size();
        }
    }

    // Tab自定义view
    public View getTabView(String title, int image_src) {
        View v = LayoutInflater.from(getApplicationContext()).inflate(R.layout.layout_tablayout, null);
        TextView textView = (TextView) v.findViewById(R.id.textview);
        textView.setText(title);
        textView.setTextSize(15);
        textView.setTextColor(getResources().getColor(R.color.hei));
        ImageView imageView = (ImageView) v.findViewById(R.id.iv_tablayout_img);
        imageView.setImageResource(image_src);
        return v;
    }
}
